#include "Settings.h"

#include <coemain.h> // CCoeEnv
#include <s32stor.h> // CDictionaryStore

#include "Test9AppUi.h"

CSettings* CSettings::iSettings = NULL;

CSettings* CSettings::theInstance()
	{
	if(iSettings == NULL)
		{return NULL;}
	return iSettings;
	}

CSettings::CSettings(CApaApplication& aApp) : iApp(aApp)
{
	iSettings = this;
}

void CSettings::ConstructL()
{
    LoadL();
}
 
CDictionaryStore* CSettings::LoadL()
{
    isPhoneMemory = true;
    stepsPerClick = 1;
    
    CDictionaryStore* iniFile = iApp.OpenIniFileLC(CCoeEnv::Static()->FsSession());
    if (iniFile->IsPresentL(KUidTest9Application))
    {
        RDictionaryReadStream readStream;
        readStream.OpenLC(*iniFile, KUidTest9Application);
 
        //TUint16 version = static_cast<TUint16> (readStream.ReadUint16L());
 
        isPhoneMemory = readStream.ReadInt16L();
        stepsPerClick = readStream.ReadInt16L();
        
        
        // Initialize other settings
 
        readStream.Close();
        CleanupStack::PopAndDestroy(&readStream);
    }
    else
    {
        // ini doesn't exist, so use default values
        iStartOnBackground = EFalse;
    }
 
    CleanupStack::PopAndDestroy(iniFile);
    return iniFile;
}
 
void CSettings::StoreL()
{
    CDictionaryStore* iniFile = iApp.OpenIniFileLC(CCoeEnv::Static()->FsSession());
    RDictionaryWriteStream writeStream;
    writeStream.AssignLC(*iniFile, KUidTest9Application);
 
    // You may support some simple versioning if you wish
    //writeStream.WriteInt16L(one);
    writeStream.WriteInt16L(isPhoneMemory);
    writeStream.WriteInt16L(stepsPerClick);
    
    writeStream.CommitL();
    iniFile->CommitL();
    CleanupStack::PopAndDestroy(2, iniFile);
}
